package cn.iocoder.yudao.module.promotion.controller.app.prizedraw;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.promotion.controller.admin.prizedraw.vo.PrizeDrawAwardsRespVO;
import cn.iocoder.yudao.module.promotion.controller.admin.prizedraw.vo.PrizeDrawRespVO;
import cn.iocoder.yudao.module.promotion.controller.app.prizedraw.vo.PrizeDrawDoingReqVO;
import cn.iocoder.yudao.module.promotion.service.prizedraw.PrizeDrawService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.PRIZE_DRAW_PLEASE_FIRST_CONFIGURE_THE_DETAILS_OF_THE_LOTTERY_RULES;

@Tag(name = "用户 APP - 抽奖活动")
@RestController
@RequestMapping("/promotion/prize-draw")
@Validated
public class AppPrizeDrawController {

    @Resource
    private PrizeDrawService prizeDrawService;

    @Operation(summary = "获取当前抽奖信息")
    @GetMapping("/getCurrentPrizeDraw")
    public CommonResult<PrizeDrawRespVO> getCurrentPrizeDraw() {
        return success(prizeDrawService.getCurrentPrizeDraw());
    }

    @Operation(summary = "获取抽奖规则")
    @GetMapping("/getPrizeDrawRuleById")
    public CommonResult<String> getPrizeDrawRuleById(@NotNull(message = "抽奖活动Id不能为空") @RequestParam(value = "prizeDrawId") Long prizeDrawId) {
        PrizeDrawRespVO prizeDrawRespVO = prizeDrawService.getInfoById(prizeDrawId);
        if (Objects.isNull(prizeDrawRespVO)) {
            throw exception(PRIZE_DRAW_PLEASE_FIRST_CONFIGURE_THE_DETAILS_OF_THE_LOTTERY_RULES);
        }
        return success(prizeDrawRespVO.getPrizeDrawRule());
    }

    @Operation(summary = "进行抽奖")
    @PostMapping("/doingPrizeDraw")
    public CommonResult<PrizeDrawAwardsRespVO> doingPrizeDraw(@RequestBody @Valid PrizeDrawDoingReqVO dto){
        dto.setUserId(getLoginUserId());
        return success(prizeDrawService.doingPrizeDraw(dto));
    }

}
